package main

import (
	"html/template"
	"io"
	"log"
	"net/http"
	"www1/lyfj"
)

func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
	var (
		t   *template.Template
		err error
	)
	if r.URL.Path == "/" {
		http.Redirect(w, r, "/login", http.StatusFound)
	}
	t, err = template.ParseFiles("template/404-1.html", "template/header.html", "template/footer.html")
	if err != nil {
		log.Println(err)
	}
	t.Execute(w, nil)
}

func loginHandler(w http.ResponseWriter, r *http.Request) {
	var (
		t      *template.Template
		cookie http.Cookie
		err    error
	)
	r.ParseForm()
	log.Printf("URL:%s, scheme:%s,method:%s\n", r.URL.Path, r.URL.Scheme, r.Method) //获取请求的方法
	if r.Method == "GET" {
		t, err = template.ParseFiles("template/login.html", "template/header.html", "template/footer.html")
		if err != nil {
			log.Println(err)
		}
		t.Execute(w, nil)
	} else if r.Method == "POST" {
		log.Println("POST数据", r.Form["password"])
		cookie = http.Cookie{Name: "testcookiename", Value: "testcookievalue", Path: "/", MaxAge: 86400}
		http.SetCookie(w, &cookie)
		log.Printf("设置cookie成功。跳转到首页\n")
		//w.Write([]byte("<b>设置cookie成功。</b>\n"))
		http.Redirect(w, r, "/index", http.StatusFound)
	} else if r.Method == "DEL" {
		log.Println("DEL数据", r.Form["password"])
	}

}
func indexHandler(w http.ResponseWriter, r *http.Request) {
	var (
		t           *template.Template
		err         error
		cookie      *http.Cookie
		cookievalue string
	)
	r.ParseForm()
	log.Printf("URL:%s, scheme:%s,method:%s\n", r.URL.Path, r.URL.Scheme, r.Method) //获取请求的方法
	if r.Method == "GET" {
		cookie, err = r.Cookie("testcookiename")
		if err == nil {
			cookievalue = cookie.Value
			log.Printf("cookie的值是：%s\n", cookievalue)
			//w.Write([]byte("<b>cookie的值是：" + cookievalue + "</b>\n"))
			t, err = template.ParseFiles("template/index.html", "template/header.html", "template/menu_gl.html", "template/footer.html")
			if err != nil {
				log.Println(err)
			}
			t.Execute(w, nil)
		} else {
			log.Printf("读取出现错误：%s\n", err.Error())
			w.Write([]byte("<b>读取出现错误：" + err.Error() + "</b>\n"))
		}

	}
}
func adminHandler(w http.ResponseWriter, r *http.Request) {
	var (
		t   *template.Template
		err error
	)
	r.ParseForm()
	log.Printf("URL:%s, scheme:%s,method:%s\n", r.URL.Path, r.URL.Scheme, r.Method) //获取请求的方法
	if r.Method == "GET" {
		t, err = template.ParseFiles("template/admin.html", "template/header.html", "template/menu_gl.html", "template/footer.html")
		if err != nil {
			log.Println(err)
		}
		t.Execute(w, nil)
	}
}

func xm_lyfjHandler(w http.ResponseWriter, r *http.Request) {
	var (
		t   *template.Template
		err error
	)
	r.ParseForm()
	log.Printf("URL:%s, scheme:%s,method:%s\n", r.URL.Path, r.URL.Scheme, r.Method) //获取请求的方法
	if r.Method == "GET" {
		t, err = template.ParseFiles("template/xm_lyfj.html", "template/header.html", "template/menu_gl.html", "template/footer.html")
		if err != nil {
			log.Println(err)
		}
		t.Execute(w, nil)
	} else if r.Method == "POST" {
		log.Println(err)
	}
}

func xm_ajax_lyfjs_Handler(w http.ResponseWriter, r *http.Request) {
	var data_json []byte
	data_json = lyfj.Get_lyfj_json(1)
	io.WriteString(w, string(data_json[:]))
}

func xm_ajax_get_delete_modal_body_Handler(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, string("hehefdklfae"))
}
